
//
//  HCMainDataModel.m
//  HelloCall
//
//  Created by WangYongpeng on 13-10-15.
//  Copyright (c) 2013年 HC. All rights reserved.
//

#import "HCNetworkManger.h"
//pause modify-#import "BusinessProtocalEngine.h"
#import "HCLoginInfoModel.h"

#define kStateDatePeriod (300)
#define kLoadDataLimit (20)

#import <set>
using namespace std;

typedef std::set< id<HCNetworkObserver> > NetworkObervers;

@interface HCNetworkManger()//pause modify--<ProtocalDelegate>
{
    NetworkObervers m_observers;
}

@end

@implementation HCNetworkManger

static HCNetworkManger* g_networkManger = nil;

+ (HCNetworkManger*)shareManger
{
    if (nil == g_networkManger)
    {
        g_networkManger = [[HCNetworkManger alloc] init];
    }
    
    return g_networkManger;
}

- (id)init
{
    self = [super init];
    if (nil != self)
    {
        //pause modify--CBusinessProtocalEngine::shareProtocalEngine()->delegate = self;
    }
    
    return self;
}

- (void)dealloc
{
    g_networkManger = nil;
    
    [super dealloc];
}

- (void)addObserver:(id <HCNetworkObserver>)observer
{
    assert(observer);
    m_observers.insert(observer);
}

- (void)removeObserver:(id <HCNetworkObserver>)observer
{
    assert(observer);
    m_observers.erase(observer);
}

- (BOOL)loginWithUserName:(NSString*)userName pwd:(NSString*)pwd
{
    if (userName.length < 1 || pwd.length < 1)
    {
        return NO;
    }
    
    //pause modify--string nameString = userName.UTF8String;
    //pause modify--string pwdString = pwd.UTF8String;
    
    //pause modify--CBusinessProtocalEngine::shareProtocalEngine()->login(nameString, pwdString);
    return YES;
}

- (BOOL)getQueryTicketStatByTicketType:(CallInfoType)ticketType
{
    if (ticketType > CallInfoType_shift)
    {
        return NO;
    }
    
    //pause modify--int stateDatePeriod = kStateDatePeriod;
    //pause modify--string userID = [HCLoginInfoModel defaultModel].userID.UTF8String;

    //pause modify--CBusinessProtocalEngine::shareProtocalEngine()->queryTicketStat(userID, ticketType, stateDatePeriod);
    return YES;
}

- (BOOL)getQueryTicketListByTicketType:(CallInfoType)ticketType offset:(int)offset
{
    if (ticketType > CallInfoType_shift)
    {
        return NO;
    }
    
    //pause modify--int stateDatePeriod = kStateDatePeriod;
    //pause modify--int limit = kLoadDataLimit;
    
    //pause modify--string userID = [HCLoginInfoModel defaultModel].userID.UTF8String;
    
    //pause modify--CBusinessProtocalEngine::shareProtocalEngine()->queryTicketList(userID, ticketType, offset, limit, stateDatePeriod);
    return YES;
}

- (BOOL)getQueryTicketListByTicketType:(CallInfoType)ticketType offset:(int)offset limit:(int)limit
{
    if (ticketType > CallInfoType_shift)
    {
        return NO;
    }
    
    //pause modify--int stateDatePeriod = kStateDatePeriod;
    
    //pause modify--string userID = [HCLoginInfoModel defaultModel].userID.UTF8String;
    
    //pause modify--CBusinessProtocalEngine::shareProtocalEngine()->queryTicketList(userID, ticketType, offset, limit, stateDatePeriod);
    return YES;
}

- (BOOL)getQueryTicketDetailByTicketID:(NSString*)ticketID
{
    if (ticketID.length < 1)
    {
        return NO;
    }
    
    //pause modify--string userIDString = [HCLoginInfoModel defaultModel].userID.UTF8String;
    //pause modify--string ticketIDString = ticketID.UTF8String;
    
    //pause modify--CBusinessProtocalEngine::shareProtocalEngine()->queryTicketDetail(userIDString, ticketIDString);
    return YES;
}

- (BOOL)applyTransferByTicketID:(NSString*)ticketID
{
    if (ticketID.length < 1)
    {
        return NO;
    }
    
    //pause modify--string userIDString = [HCLoginInfoModel defaultModel].userID.UTF8String;
    //pause modify--string ticketIDString = ticketID.UTF8String;
    
    //pause modify--CBusinessProtocalEngine::shareProtocalEngine()->applyTransfer(userIDString, ticketIDString);
    return YES;
}

- (BOOL)applyGiveupByTicketID:(NSString*)ticketID
{
    if (ticketID.length < 1)
    {
        return NO;
    }
    
    //pause modify--string userIDString = [HCLoginInfoModel defaultModel].userID.UTF8String;
    //pause modify--string ticketIDString = ticketID.UTF8String;
    
    //pause modify--CBusinessProtocalEngine::shareProtocalEngine()->applyGiveup(userIDString, ticketIDString);
    return YES;
}

- (BOOL)recordCustInfoByTicketID:(NSString*)ticketID
{
    if (ticketID.length < 1)
    {
        return NO;
    }
    
    //pause modify--string userIDString = [HCLoginInfoModel defaultModel].userID.UTF8String;
    //pause modify--string ticketIDString = ticketID.UTF8String;
    
    //pause modify--CBusinessProtocalEngine::shareProtocalEngine()->recordCustInfo(userIDString, ticketIDString);
    return YES;
}

- (BOOL)queryCustState
{
    //pause modify--int stateDatePeriod = kStateDatePeriod;
    //pause modify--string userIDString = [HCLoginInfoModel defaultModel].userID.UTF8String;
    
    //pause modify--CBusinessProtocalEngine::shareProtocalEngine()->queryCustStat(userIDString, stateDatePeriod);
    return YES;
}

- (BOOL)queryCustListByOffset:(int)offset
{
    if (offset < 0)
    {
        return NO;
    }
    
    //pause modify--int limit = kLoadDataLimit;
    //pause modify--int stateDatePeriod = kStateDatePeriod;
    //pause modify--string userIDString = [HCLoginInfoModel defaultModel].userID.UTF8String;
    
    //pause modify--CBusinessProtocalEngine::shareProtocalEngine()->queryCustList(userIDString, stateDatePeriod, offset, limit);
    return YES;
}

- (BOOL)queryCustDetailByCustomId:(NSString*)customId
{
    if (customId.length < 1)
    {
        return NO;
    }
    
    //pause modify--string userIDString = [HCLoginInfoModel defaultModel].userID.UTF8String;
    //pause modify--string customIDString = customId.UTF8String;
    
    //pause modify--CBusinessProtocalEngine::shareProtocalEngine()->queryCustDetail(userIDString, customIDString);
    return YES;
}

- (BOOL)queryCallMemoByCustomId:(NSString*)customId offset:(int)offset
{
    if (customId.length < 1 || offset < 0)
    {
        return NO;
    }
    
    //pause modify--int limit = kLoadDataLimit;
    //pause modify--string userIDString = [HCLoginInfoModel defaultModel].userID.UTF8String;
    //pause modify--string customIDString = customId.UTF8String;
    
    //pause modify--CBusinessProtocalEngine::shareProtocalEngine()->queryCallMemo(userIDString, customIDString, offset, limit);
    return YES;
}

- (BOOL)updateCallMemoByCustomId:(NSString*)customId content:(NSString*)content
{
    if (customId.length < 1 || content.length < 1)
    {
        return NO;
    }
    
    //pause modify--string userIDString = [HCLoginInfoModel defaultModel].userID.UTF8String;
    //pause modify--string customIDString = customId.UTF8String;
    //pause modify--string contentString = content.UTF8String;
    
    //pause modify--CBusinessProtocalEngine::shareProtocalEngine()->updateCallMemo(userIDString, customIDString, contentString);
    return YES;
}

- (BOOL)queryCallBackInfoByCustomId:(NSString*)customId offset:(int)offset
{
    if (customId.length < 1 || offset < 0)
    {
        return NO;
    }
    
    //pause modify--int limit = kLoadDataLimit;
    //pause modify--string userIDString = [HCLoginInfoModel defaultModel].userID.UTF8String;
    //pause modify--string customIDString = customId.UTF8String;
    
    //pause modify--CBusinessProtocalEngine::shareProtocalEngine()->queryCallBackInfo(userIDString, customIDString, offset, limit);
    return YES;
}

- (BOOL)updateCallBackInfoByCallBackId:(NSString*)callBackId remark:(NSString*)remark callBackTime:(NSString*)callBackTime
{
    if (callBackId.length < 1 || remark.length < 1 || callBackTime.length < 1)
    {
        return NO;
    }
    
    //pause modify--string userIDString = [HCLoginInfoModel defaultModel].userID.UTF8String;
    //pause modify--string callBackIdString = callBackId.UTF8String;
    //pause modify--string remarkString = remark.UTF8String;
    //pause modify--string callBackTimeString = callBackTime.UTF8String;
    
    //pause modify--CBusinessProtocalEngine::shareProtocalEngine()->updateCallBackInfo(userIDString, callBackIdString, remarkString, callBackTimeString);
    return YES;
}

- (BOOL)setCallBackStatusByCallBackId:(NSString*)callBackId status:(NSString*)status
{
    if (callBackId.length < 1 || status.length < 1)
    {
        return NO;
    }
    
    //pause modify--string userIDString = [HCLoginInfoModel defaultModel].userID.UTF8String;
    //pause modify--string callBackIdString = callBackId.UTF8String;
    //pause modify--string statusString = status.UTF8String;
    
    //pause modify--CBusinessProtocalEngine::shareProtocalEngine()->setCallBackStatus(userIDString, callBackIdString, statusString);
    return YES;
}

- (BOOL)setCustStatusByCustomId:(NSString*)customId status:(NSString*)status
{
    if (customId.length < 1 || status.length < 1)
    {
        return NO;
    }
    
    //pause modify--string userIDString = [HCLoginInfoModel defaultModel].userID.UTF8String;
    //pause modify--string customIdString = customId.UTF8String;
    //pause modify--string statusString = status.UTF8String;
    
    //pause modify--CBusinessProtocalEngine::shareProtocalEngine()->setCustStatus(userIDString, customIdString, statusString);
    return YES;
}

- (void)requestComm

{
    
    //1、设置代理
    
    //pause modify--CBusinessProtocalEngine::shareProtocalEngine()->delegate = self;
    
    //2、调用登录方法
    
    //pause modify--CBusinessProtocalEngine::shareProtocalEngine()->login("111", "111");
    
    //CBusinessProtocalEngine::shareProtocalEngine()->queryTicketDetail("111", "1");
    
    //CBusinessProtocalEngine::shareProtocalEngine()->queryTicketList("111", 1, "2013-09-01 00:00:00", "2013-09-30 23:59:59", 0, 100);
    
    //CBusinessProtocalEngine::shareProtocalEngine()->applyTransfer("111", "1");
    
    //CBusinessProtocalEngine::shareProtocalEngine()->applyGiveup("111", "1");
    
    //CBusinessProtocalEngine::shareProtocalEngine()->queryCustStat("111", 1, "2013-09-01 00:00:00", "2013-09-01 00:00:00");
    
    //CBusinessProtocalEngine::shareProtocalEngine()->queryCustList("111", 1, "2013-09-01 00:00:00", "2013-09-30 23:59:59", 0, 100);
    
}



//解析完成回调函数
- (void)notifyMsg:(int)msgID data:(void*) data
{
    
    for (NetworkObervers::iterator it = m_observers.begin();
         it != m_observers.end();
         it++)
    {
        if ([*it respondsToSelector:@selector(notifyNetworkMsg:data:)])
        {
            [*it notifyNetworkMsg:msgID data:data];
        }
    }
    
    switch (msgID) {
            
//        case ELogin:
//        {
//            break;
//        }
//        case EQueryTicketStat:
//        {
//            queryTicketStateResp* resp = (queryTicketStateResp*)data;
//            
//            if (!resp->headResp.success)
//            {
//                //显示错误提示
//                break;
//            }
//            
//            [HCCallListModel defaultModel].assignNumCount = resp->stateBean.assignNumCount;
//            [HCCallListModel defaultModel].callbackNumCount = resp->stateBean.callbackNumCount;
//            [HCCallListModel defaultModel].tgNumCount = resp->stateBean.tgNumCount;
//            break;
//        }
//        case EQueryTicketList:
//        {
//            queryTicketListResp* resp = (queryTicketListResp*)data;
//            
//            if (!resp->headResp.success)
//            {
//                //显示错误提示
//                break;
//            }
//            
//            for (int i = 0; i < resp->vecDetail.size(); i++)
//            {
//                HCCallInfo* info = [[HCCallInfo alloc] init];
//                info.userID = [NSString stringWithFormat:@"%d",resp->vecDetail.at(i).ticketeIdticketId];
//                info.phoneNumber= [NSString stringWithUTF8String:resp->vecDetail.at(i).custNum.data()];
//                NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
//                [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//                info.initTime = [dateFormatter dateFromString:[NSString stringWithUTF8String:resp->vecDetail.at(i).firstContactTime.data()]];
//                [dateFormatter release];
//                info.callSource = [NSString stringWithUTF8String:resp->vecDetail.at(i).numSource.data()];
//                info.callNumber = resp->vecDetail.at(i).callTimes;
//                info.sumCallTime = resp->vecDetail.at(i).callTimeLong;
//                [[HCCallListModel defaultModel] addCallInfo:info type:(CallInfoType)resp->ticketType];
//                [info release];
//            }
//            
//            break;
//        }
//        case EQueryTicketDetail:
//        {
//            queryTicketDetailResp* resp = (queryTicketDetailResp*)data;
//            
//            if (!resp->headResp.success)
//            {
//                //显示错误提示
//                break;
//            }
//            
//            HCCallInfo* info = [[HCCallListModel defaultModel] getCallInfobyUserId:[NSString stringWithFormat:@"%d", resp->ticketId]];
//            
//            info.phoneNumber= [NSString stringWithUTF8String:resp->custNum.data()];
//            NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
//            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//            info.initTime = [dateFormatter dateFromString:[NSString stringWithUTF8String:resp->firstcontacttime.data()]];
//            [dateFormatter release];
//            info.callSource = [NSString stringWithUTF8String:resp->numSource.data()];
//            info.callNumber = resp->callTimes;
//            info.sumCallTime = resp->callTimeLong;
//            [[HCCallListModel defaultModel] modifyCallInfo:info byUserId:[NSString stringWithFormat:@"%d", resp->ticketId]];
//            
//            break;
//        }
//        case EApplyTransfer:
//        {
//            applyTransferResp* resp = (applyTransferResp*)data;
//            
//            if (!resp->headResp.success)
//            {
//                //显示错误提示
//                break;
//            }
//            break;
//        }
//        case EApplyGiveup:
//        {
//            applyGiveupResp* resp = (applyGiveupResp*)data;
//            
//            if (!resp->headResp.success)
//            {
//                //显示错误提示
//                break;
//            }
//            break;
//        }
//        case ERecordCustInfo:
//        {
//            break;
//        }
//        case EQueryCustStat:
//        {
//            queryCustStatResp* resp = (queryCustStatResp*)data;
//            
//            if (!resp->headResp.success)
//            {
//                //显示错误提示
//                break;
//            }
//            
//            //增加处理逻辑
//            break;
//        }
//        case EQueryCustList:
//        {
//            queryCustListResp* resp = (queryCustListResp*)data;
//            
//            if (!resp->headResp.success)
//            {
//                //显示错误提示
//                break;
//            }
//            
//            for (int i = 0; i < resp->vecBean.size(); i++)
//            {
//                HCCRMCustomInfo* info = [[HCCRMCustomInfo alloc] init];
//                
//                info.userID = [NSString stringWithFormat:@"%d",resp->vecBean.at(i).custId];
//                info.mobilePhoneNumber = [NSString stringWithUTF8String:resp->vecBean.at(i).mobile.data()];
//                info.contact = [NSString stringWithUTF8String:resp->vecBean.at(i).name.data()];
//                info.company = [NSString stringWithUTF8String:resp->vecBean.at(i).company.data()];
//                info.qqNumber = [NSString stringWithUTF8String:resp->vecBean.at(i).qq.data()];
//                info.email = [NSString stringWithUTF8String:resp->vecBean.at(i).email.data()];
//                info.phoneNumber = [NSString stringWithUTF8String:resp->vecBean.at(i).fax.data()];
//                info.sourceType = (CallSourceType)resp->vecBean.at(i).type;
//                NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
//                [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//                info.initTime = [dateFormatter dateFromString:[NSString stringWithUTF8String:resp->vecBean.at(i).c_time.data()]];
//                [dateFormatter release];
//                ECustomType customType = (ECustomType)resp->vecBean.at(i).status;
//                customType = ECustomType_Follow;
//                [[HCSuperCRMModel defaultModel] addCustomInfo:info customType:customType];
//                
//                [info release];
//            }
//            break;
//        }
//        case EQueryCustDetail:
//        {
//            queryCustDetailResp* resp = (queryCustDetailResp*)data;
//            
//            if (!resp->headResp.success)
//            {
//                //显示错误提示
//                break;
//            }
//            
//            NSString* customID = [NSString stringWithFormat:@"%d",resp->bean.custId];
//            ECustomType customType = (ECustomType)resp->bean.status;
//            HCCRMCustomInfo* info = [[[HCSuperCRMModel defaultModel] getCustomInfoByID:customID customType:customType] retain];
//            
//            info.mobilePhoneNumber = [NSString stringWithUTF8String:resp->bean.mobile.data()];
//            info.contact = [NSString stringWithUTF8String:resp->bean.name.data()];
//            info.company = [NSString stringWithUTF8String:resp->bean.company.data()];
//            info.qqNumber = [NSString stringWithUTF8String:resp->bean.qq.data()];
//            info.email = [NSString stringWithUTF8String:resp->bean.email.data()];
//            info.phoneNumber = [NSString stringWithUTF8String:resp->bean.fax.data()];
//            info.sourceType = (CallSourceType)resp->bean.type;
//            NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
//            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//            info.initTime = [dateFormatter dateFromString:[NSString stringWithUTF8String:resp->bean.c_time.data()]];
//            [dateFormatter release];
//            
//            [[HCSuperCRMModel defaultModel] modifyCustomInfo:info customID:customID];
//            
//            [info release];
//            break;
//        }
//        case EQueryCallMemo:
//        {
//            break;
//        }
//        case ERecordCallMemo:
//        {
//            break;
//        }
//        case EUpdateCallMemo:
//        {
//            break;
//        }
//        case EUpdateCustInfo:
//        {
//            break;
//        }
//        case ESetCallBackTime:
//        {
//            break;
//        }
//        case ESetCustStatus:
//        {
//            break;
//        }
//        case EQueryCallBackInfo:
//        {
//            break;
//        }
//        case EUpdateCallBackInfo:
//        {
//            break;
//        }
//        case ESetCallBackStatus:
//        {
//            break;
//        }
    }
    
}




@end
